package com.maple.utils.util

import android.text.TextUtils
import com.blankj.utilcode.util.AppUtils
import com.maple.utils.const.Global
import com.maple.utils.const.ConstKey
import com.tencent.mmkv.MMKV
import java.util.*

class HttpRequestUtils {


    companion object{

        val versionCode: Int by lazy {
            AppUtils.getAppVersionCode()
        }

        val versionName: String by lazy {
            AppUtils.getAppVersionName()
        }


        private val mmkv: MMKV by lazy {
            MMKV.defaultMMKV()
        }

        fun requestHeader(appName: String): WeakHashMap<String, Any> {
            val header: WeakHashMap<String, Any> = WeakHashMap()
            val token = getAccessToken()
            header.put(Global.TOKEN, token)
            header.put(Global.APP_ID_KEY, EncryptDecodeUtils.APP_ID)
            header.put(Global.APP_NAME, appName)
            header.put(Global.APP_VER, versionName)
            return header
        }

        fun requestParams(): WeakHashMap<String, Any> {
            val params: WeakHashMap<String, Any> = WeakHashMap()
//            val token = getAccessToken()
//            params.put("source", source)
//            params.put("versionId", versionId)
//            params.put(Global.TOKEN, token)
            return params
        }

        fun putAccessToken(token: String?) {
            if(!TextUtils.isEmpty(token)) {
                mmkv.putString(ConstKey.SaveInfoKey.ACCESS_TOKEN, token)
            }
        }

        fun getAccessToken(): String {
            return mmkv.getString(ConstKey.SaveInfoKey.ACCESS_TOKEN,"")?: ""
        }

        fun clearAccessToken() {
            mmkv.putString(ConstKey.SaveInfoKey.ACCESS_TOKEN, "")
        }
    }
}